<template>
  <div>
    <el-dialog v-bind="$attrs" v-on="$listeners" @open="onOpen" @close="onClose" title="新增权限">
      <el-row :gutter="15">
        <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
          <el-col :span="12">
            <el-form-item label="权限code" prop="perCode">
              <el-input
                v-model="formData.perCode"
                placeholder="请输入本文行单权限code"
                clearable
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="权限名称" prop="perName">
              <el-input
                v-model="formData.perName"
                placeholder="请输入本文行单权限名称"
                clearable
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="前端路由" prop="perRoute">
              <el-input
                v-model="formData.perRoute"
                placeholder="请输入本文行单前端路由"
                clearable
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上级权限" prop="perPname">
              <el-input v-model="formData.perPname" readonly clearable :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="权限排序" prop="sort">
              <el-input-number v-model="formData.sort" placeholder="权限排序" :step="1"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="权限类型" prop="perType">
              <el-select
                v-model="formData.perType"
                placeholder="请选择权限类型"
                clearable
                :style="{width: '100%'}"
              >
                <el-option
                  v-for="(item, index) in perTypeOption"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属客户端" prop="perClient">
              <el-select
                v-model="formData.perClient"
                placeholder="请选择所属客户端"
                clearable
                :style="{width: '100%'}"
              >
                <el-option
                  v-for="(item, index) in perClientOption"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import enums from "@/assets/data/enums.js";
import { put } from "@/api/permission";
export default {
  inheritAttrs: false,
  components: {},
  props: ["currentRow"],
  data() {
    return {
      formData: {
        perCode: undefined,
        perName: undefined,
        perRoute: undefined,
        sort: undefined,
        perType: undefined,
        perClient: undefined,
        perPname: undefined
      },
      rules: {
        perCode: [
          {
            required: true,
            message: "请输入本文行单权限code",
            trigger: "blur"
          }
        ],
        perName: [
          {
            required: true,
            message: "请输入本文行单权限名称",
            trigger: "blur"
          }
        ],
        perRoute: [
          {
            required: true,
            message: "请输入本文行单前端路由",
            trigger: "blur"
          }
        ],
        sort: [
          {
            required: true,
            message: "权限排序",
            trigger: "blur"
          }
        ],
        perType: [
          {
            required: true,
            message: "请选择权限类型",
            trigger: "change"
          }
        ],
        perClient: [
          {
            required: true,
            message: "请选择所属客户端",
            trigger: "change"
          }
        ]
      },
      perTypeOption: enums.perTypeOptions,
      perClientOption: enums.clientType
    };
  },
  methods: {
    onOpen() {
      if (this.currentRow) {
        this.formData.perPname = this.currentRow.perName;
      } else {
        this.formData.perPname = "顶级";
      }
    },
    onClose() {
      this.$refs["elForm"].resetFields();
    },
    close() {
      this.$emit("update:visible", false);
    },
    handelConfirm() {
      this.$refs["elForm"].validate(valid => {
        if (!valid) return;
        this.formData.perPcode = this.currentRow
          ? this.currentRow.perCode
          : "0";
        put(this.formData)
          .then(res => {
            this.$emit("addSuccess", this.formData.perPcode);
            this.close();
          })
          .catch(err => {
            this.$emit("addSuccess", this.formData.perPcode);
            this.close();
          });
      });
    }
  }
};
</script>
<style>
</style>
